﻿using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace NursingFormSystem.BloodGlucoseMeterNICU
{
    public partial class HistoryBloodGlucoseMeterNICU : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        #endregion

        #region 构造函数
        public HistoryBloodGlucoseMeterNICU(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            //成员变量
            LoginUser = loginUser;
            PatientInfo = patientInfo;
            //绑定数据
            BindControl();
            BindInfo();
        }
        #endregion

        #region 查询
        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        #endregion

        #region 新增
        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddBloodGlucoseMeterNICU addForm = new AddBloodGlucoseMeterNICU(LoginUser, PatientInfo, string.Empty);
            addForm.ShowDialog();
            if (addForm.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region 修改
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddBloodGlucoseMeterNICU addForm = new AddBloodGlucoseMeterNICU(LoginUser, PatientInfo, Id);
                addForm.ShowDialog();
                if (addForm.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion

        #region 打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            BloodGlucoseMeterPrintsNICU f = new BloodGlucoseMeterPrintsNICU(LoginUser, PatientInfo, StatDate.DateTime, EndDate.DateTime);
            f.Show();
        }
        #endregion

        #region 删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    string dateStart = this.StatDate.Text;
                    string dateEnd = this.EndDate.Text;
                    BindInfo();
                }
            }
        }
        #endregion

        #region  开始日期：当天-30天，结束日期：为当天日期
        public void BindControl()
        {
            this.StatDate.DateTime = DateTime.Now.AddDays(-30);
            this.EndDate.DateTime = DateTime.Now;
        }
        #endregion

        #region 查询共用方法
        private void BindInfo()
        {
            if (this.PatientInfo == null) return;
            string patientid = this.PatientInfo.PATIENT_ID.Trim();   //住院号
            int VisitId = this.PatientInfo.VISIT_ID;   //住院次数
            int FormId = (int)FormType.BloodGlucoseMeterNICU;    //表单ID
            DataTable dt = new DataTable();
            dt.Columns.Add("JianCeRiQi", typeof(string));
            dt.Columns.Add("ShiJian", typeof(string));
            dt.Columns.Add("XueTangZhi", typeof(string));
            dt.Columns.Add("QianMing", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, StatDate.DateTime, EndDate.DateTime);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    BloodMeter bom = new BloodMeter();
                    bom = XmlUtil.XMLToObject<BloodMeter>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    newRow["JianCeRiQi"] = Convert.ToDateTime(bom.JianCeRiQi).ToString("yyyy-MM-dd");     //日期
                    newRow["ShiJian"] = DateTimeHelper.HHmmssToHHmm(bom.ShiJian);     //时间
                    newRow["XueTangZhi"] = bom.XueTangZhi;
                    newRow["QianMing"] = bom.QianMing;
                    newRow["Id"] = record.Id;   //ID隐藏，双击触发事件
                    dt.Rows.Add(newRow);
                }
            }
            dt.DefaultView.Sort = "JianCeRiQi DESC,ShiJian DESC";
            DataTable data = dt.DefaultView.ToTable();
            gridControl1.DataSource = data;
        }
        #endregion

        #region 双击事件
        private void bandedGridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                if (bandedGridView1.RowCount > 0)
                {
                    string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                    AddBloodGlucoseMeterNICU addg = new AddBloodGlucoseMeterNICU(LoginUser, PatientInfo, Id);
                    addg.ShowDialog();
                    if (addg.DialogResult == DialogResult.OK)
                    {
                        BindInfo();
                    }
                }
            }
        }
        #endregion

    }
}